/**
 * Created by zhulizhe on 2017/8/28.
 */
import { Platform } from 'react-native';

const AppKey = "cf385992c3fc46cbaebae2c1dae08653"
const AppSecret = "5e5cd8e3ccca45c2a5a3b00a5a90cdd5"
import loginStore from '../../luojs/store/LoginStoreLuo'
export const Base_url = "http://www.huipay.com:9931/huibeiwater/"
//export const Base_url = "http://123.57.161.212:9931/huibeiwater/"
// const Image_Base_url = "http://192.168.1.228:9936/"

class NetUtil {

    async requestData(url, param, method = 'POST', jsonFlg=true) {
        let accessInfo = await this.getAccessInfo();
        let body = null;

        if (param != null) {
            body = param;
            if (accessInfo && accessInfo != undefined) {
                body.accessInfo = accessInfo;
                body.os = Platform.OS ;
                body.osversion = Platform.Version ;
            }
        }
        LOG('## url:' + Base_url + url+JSON.stringify(body));
        return new Promise((resolve, reject)=>{
            fetch(Base_url + url,
                {
                    method: method,
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(body)
                }).then((response) => {
                LOG('##response param :' + JSON.stringify(response));
                if (response.status !== 200){
                    reject && reject(JSON.parse(response._bodyText).message);
                }else{
                    if (jsonFlg){
                        resolve(JSON.parse(response._bodyText));
                    }else {
                        resolve(response._bodyText);
                    }
                }
            }).catch((error)=>{
                resolve(error);
            });
        });
    }

    // 图片上传
    uploadImage(filePaths, callback) {
        let uploadUrl = 'http://123.57.161.212:9936/imageserver/upload';
        LOG('uploadImage', filePaths);
        LOG('uploadImage', uploadUrl);
        let formData = new FormData();
        filePaths.map((filePath, index) => {
            formData.append('file', {
                uri: filePath.uri,
                type: 'multipart/form-data',
                name: 'jpg'
            })
        })
        fetch(uploadUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'multipart/form-data'
            },
            body: formData,
        }).then((response) => response.json())
            .then((responseJSON) => {
                callback(responseJSON)
            }).done();
    }

    // 获取token
    async getAccessInfo() {
        let user = await loginStore.user();
        return new Promise(function (res, rej) {
            if (user) {
                res({
                    access_secret: user.access_token_secret,
                    access_token: user.access_token,
                    app_key:''
                })
            }
            else {
                res(false);
            }
        })
    }



}

const RequestUtil = new NetUtil();
module.exports = RequestUtil;
